/**
 * Copyright (c) 2009 Dave Ross <dave@csixty4.com>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 **/

/////////////////////////////
// Outdated Browser Testing
/////////////////////////////

var debugMode = false;

/**
 * Display an outdated browser warning if appropriate
 */
jQuery(document).ready( function() {
	if(isOutdatedBrowser() || debugMode)
	{
		if(getCookie('seen_browser_warning') == '' || debugMode)
		{
			var iconPath = jQuery('img#outdated_browser_icon').attr('src');
			jQuery('img#outdated_browser_icon').attr('src', iconPath + '/' + outdatedBrowserIconFilename());
			jQuery('a#outdated_browser_link').attr('href', outdatedBrowserUpgradeURL());
			
			jQuery('div#outdated_browser').attr('display', 'block').slideDown();
			
			// Hide the warning after 10 seconds and set a cookie
			// so it's not displayed again
			setTimeout( function() {
				jQuery('div#outdated_browser').slideUp('slow');
			}, 10000);
			
			setCookie('seen_browser_warning', true, 365)
		}
	}
});

/**
 * @see http://www.javascriptkit.com/javatutors/navigator.shtml
 */
function isOutdatedBrowser()
{
	// Firefox
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		var ffversion=new Number(RegExp.$1);
		if(ffversion < 3.0)
			return true;
		else
			return false;
	}
	
	// IE
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		var ieversion=new Number(RegExp.$1);
		if(ieversion < 7.0)
			return true;
		else
			return false;
	}
	
	// Safari 3+
	if (/Version[\/\s](\d+\.\d+)(.+)Safari/.test(navigator.userAgent))
	{
		var safariVersion=new Number(RegExp.$1);
		if(safariVersion < 3.0)
			return true;
		else
			return false;
	}
	
	// Safari < 3
	if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		return true;
	}
	
	return false;
}

function outdatedBrowserUpgradeURL()
{
	// Firefox
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		return "http://www.getfirefox.com";
	}
	
	// IE
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		return "http://www.microsoft.com/windows/downloads/ie/getitnow.mspx";
	}

	// Safari 3+	
	if (/Version[\/\s](\d+\.\d+)(.+)Safari/.test(navigator.userAgent))
	{
		return "http://www.apple.com/safari/download";	
	}
	
	// Safari < 3
	if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		return "http://www.apple.com/safari/download";	
	}
	
	return false;
}

function outdatedBrowserIconFilename()
{
	// Firefox
	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		return "firefox_icon_mini.gif";
	}
	
	// IE
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
	{
		return "ie_icon_mini.gif";
	}
	
	// Safari 3+
	if (/Version[\/\s](\d+\.\d+)(.+)Safari/.test(navigator.userAgent))
	{
		return "safari_icon_mini.gif";	
	}
	
	// Safari < 3
	if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent))
	{
		return "safari_icon_mini.gif";
	}
	
	return false;
}

/////////////////////
// Cookie Utilities
/////////////////////

/**
 * @see http://www.w3schools.com/JS/js_cookies.asp
 */
function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

/**
 * @see http://www.w3schools.com/JS/js_cookies.asp
 */
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}